package com.zhong.gang.pda.ui.action.receive.controller;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.zhong.gang.applib.http.BaseDataBean;
import com.zhong.gang.applib.http.callback.DataCallBack;
import com.zhong.gang.applib.http.request.RequestCall;
import com.zhong.gang.applib.rx.RxManager;
import com.zhong.gang.pda.api.Api;
import com.zhong.gang.pda.api.HttpUtils;
import com.zhong.gang.pda.db.loction.LocationMessageUtils;
import com.zhong.gang.pda.db.loction.LoctionMessage;
import com.zhong.gang.pda.ui.action.HuoBean;
import com.zhong.gang.pda.ui.main.MainBean;

import java.io.File;
import java.util.List;

import okhttp3.Call;

public class ReceiveUpDataPersenter {

    public void startData(Activity activity){
        List<LoctionMessage> data = LocationMessageUtils.getIntent(activity).getList();
        for (LoctionMessage bean:data){
            upStartData(activity,bean.getHuoBean(),false);
        }
    }

    public synchronized void upStartData(Activity activity, HuoBean bean, boolean showDialog){
        if (bean == null)
            return;
        if (bean.getOrderReceive() == null)
            return;
        if (!bean.getOrderReceive().getPhoto().startsWith("http")) {
            upImg(activity,bean,showDialog);
        }else upMsg(activity,bean,showDialog);
    }

    /**
     * 上传图片
     */
    private void upImg(Activity activity, HuoBean bean,boolean showDialog){
        File file = new File(bean.getOrderReceive().getPhoto());
        if (file.exists()) {
            RequestCall call = HttpUtils.post(Api.upImg, activity)
                    .addFile("file", file.getName(), file).build();
            if (showDialog)
                call.execute(activity,"上传图片",new ImgCallBack(activity,bean,showDialog));
            else call.execute(new ImgCallBack(activity,bean,showDialog));
        }else upMsg(activity,bean,showDialog);
    }

    private class ImgCallBack extends DataCallBack<BaseDataBean<String>>{
        private Activity activity;
        private HuoBean bean;
        private boolean showDialog;

        public ImgCallBack(Activity activity,HuoBean bean,boolean showDialog) {
            super(activity);
            this.activity = activity;
            this.bean = bean;
            this.showDialog = showDialog;
        }

        @Override
        public void onResponse(BaseDataBean<String> data) throws Exception {
            bean.getOrderReceive().setPhoto(data.getResult());
            upMsg(activity,bean,showDialog);
        }
    }

    /**
     * 上传信息
     */
    private void upMsg(Activity activity,HuoBean bean,boolean showDialog){
        RequestCall call = HttpUtils.post(Api.addorderReceive,activity)
                .addParams("poundOrderId",bean.getId()+"")
                .addParams("consignorId",bean.getOrderReceive().getConsignor().getId()+"")
                .addParams("materialTypeList",new Gson().toJson(bean.getOrderReceive().getOrderMaterialTypeDetailList()))
                .addParams("slagWeight", bean.getOrderReceive().getSlagWeight()+"")
                .addParams("upDownType",String.valueOf(bean.getOrderReceive().getFloatType()))
                .addParams("upDownMoney",Math.abs(bean.getOrderReceive().getFloatAmount())+"")
                .addParams("goodsUrl",bean.getOrderReceive().getPhoto())
                .addParams("remark",bean.getOrderReceive().getRemark())
                .addParams("printTicker",bean.getOrderReceive().getPrintTicker()+"")
                .addParams("consignorBankCardId",bean.getOrderReceive().getConsignorBankCard().getId()+"")
                .build();
        if (showDialog)
            call.execute(activity,"上传信息",new MsgCallBack(activity,bean,showDialog));
        else
            call.execute(new MsgCallBack(activity,bean,showDialog));
    }


    private class MsgCallBack extends DataCallBack<BaseDataBean<String>>{
        private Activity activity;
        private HuoBean bean;
        private boolean showDialog;
        public MsgCallBack(Activity activity,HuoBean bean,boolean showDialog) {
            super(activity);
            this.activity = activity;
            this.bean = bean;
            this.showDialog = showDialog;
        }

        @Override
        public void onResponse(BaseDataBean<String> data) throws Exception {
            super.onResponse(data);
            LocationMessageUtils.getIntent(activity).delet(bean);
            if (showDialog)
                activity.finish();
        }
        @Override
        public void onError(Call call, Exception e, int id) {
            super.onError(call, e, id);
            LocationMessageUtils.getIntent(activity).saveData(bean);
            if (showDialog)
                activity.finish();
        }
    }
}
